﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace bttuan5_hdh_bai2
{
    public partial class Form1 : Form
    {
        DirectoryInfo curDir;
        public Form1()
        {
            InitializeComponent();

            // them cot vao listview
            ltv_thu_muc.Columns.Add("Name", 200, HorizontalAlignment.Left);
            ltv_thu_muc.Columns.Add("Size", 80, HorizontalAlignment.Right);
            ltv_thu_muc.Columns.Add("Type", 80, HorizontalAlignment.Left);
            ltv_thu_muc.Columns.Add("Date Modified", 160, HorizontalAlignment.Left);
            // hien thi theo dang chi tiet
            ltv_thu_muc.View = View.Details;
            // them danh sach hinh cho icon cua list view
            tvw_dir.ImageList = new ImageList();
            tvw_dir.ImageList.Images.Add(new Icon ("icons/may_tinh.ico"));
            tvw_dir.ImageList.Images.Add(new Icon ("icons/o_dia.ico"));
            tvw_dir.ImageList.Images.Add(new Icon ("icons/thu_muc_dong.ico"));
            tvw_dir.ImageList.Images.Add(new Icon ("icons/thu_muc_mo.ico"));
            tvw_dir.ImageList.Images.Add(new Icon ("icons/tap_tin.ico"));

            // them nut My computer va cac o dia

            TreeNode my_computer_node = new TreeNode("My computer");
            my_computer_node.Tag = "My computer";
            my_computer_node.ImageIndex = 0;
            my_computer_node.SelectedImageIndex = 0;
            tvw_dir.Nodes.Add(my_computer_node);

            // them cac o dia vao my_computer_node
            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                TreeNode drive_node = new TreeNode(drive.Name);
                drive_node.Tag = drive.RootDirectory;
                drive_node.ImageIndex = 1;
                drive_node.SelectedImageIndex = 1;
                my_computer_node.Nodes.Add(drive_node);
            }


            ltv_thu_muc.SmallImageList = new ImageList();
            ltv_thu_muc.SmallImageList.Images.Add(new Icon("icons/thu_muc.ico"));
            ltv_thu_muc.SmallImageList.Images.Add(new Icon("icons/tap_tin.ico"));

        }

        private void OpenDirectory()
        {
            //tb_dia_chi.Text = curDir.FullName;
            ltv_thu_muc.Items.Clear();
            foreach (DirectoryInfo subDir in curDir.GetDirectories())
            {
                ListViewItem lvi = ltv_thu_muc.Items.Add(subDir.Name);
                lvi.Tag = subDir;
                lvi.ImageIndex = 0;
                lvi.SubItems.Add("");
                lvi.SubItems.Add("Folder");
                lvi.SubItems.Add(subDir.LastWriteTime.ToString());
            }
            foreach (FileInfo file in curDir.GetFiles())
            {
                ListViewItem lvi = ltv_thu_muc.Items.Add(file.Name);
                lvi.Tag = file;
                lvi.ImageIndex = 1;
                lvi.SubItems.Add(file.Length.ToString());
                lvi.SubItems.Add("File");
                lvi.SubItems.Add(file.LastWriteTime.ToString());
            }
        }


        private void ltv_thu_muc_ItemActivate(object sender, EventArgs e)
        {
            if (ltv_thu_muc.SelectedItems[0].Tag.GetType() == typeof(DirectoryInfo))
            {
                curDir = (DirectoryInfo)ltv_thu_muc.SelectedItems[0].Tag;
                OpenDirectory();
            }
            else
            {
                FileInfo file = (FileInfo)ltv_thu_muc.SelectedItems[0].Tag;
                System.Diagnostics.Process.Start(file.FullName);
            }
        }

        private void view_Click(object sender, EventArgs e)
        {
            ltv_thu_muc_ItemActivate(sender, e);
        }

        private void edit_Click(object sender, EventArgs e)
        {
            ltv_thu_muc_ItemActivate(sender, e);
        }

        private void RecursiveDelete(string path, string name)
        {
            foreach (string directory in Directory.GetDirectories(path))
            {
                if (directory.EndsWith("\\" + name))
                {
                    Directory.Delete(directory, true);
                }
                else
                {
                    RecursiveDelete(directory, name);
                }
            }
        }

        private void tvw_dir_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selectedNode = tvw_dir.SelectedNode;

            if (selectedNode.Tag.GetType() == typeof(DirectoryInfo))
            {
                selectedNode.Nodes.Clear();

                DirectoryInfo dir = (DirectoryInfo)selectedNode.Tag;
                foreach (DirectoryInfo sub_dir in dir.GetDirectories())
                {
                    TreeNode dir_node = new TreeNode(sub_dir.Name);
                    dir_node.Tag = sub_dir;
                    dir_node.ImageIndex = 2;
                    dir_node.SelectedImageIndex = 3;
                    selectedNode.Nodes.Add(dir_node);
                }
                curDir = dir;
                OpenDirectory();
            }
            selectedNode.Expand();
        }
    }
}
